<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @add="addItem" :arr="list"></TodoHeader>
    <TodoMain :arr="showList" @del="delItem"></TodoMain>
    <TodoFooter :arr="showList" @changeType = "changeType" @clearCompleted="clearCompleted"></TodoFooter>
  </section>
</template>

<script>

// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"

import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
import { del } from "vue";

export default {

  data() {
    return {
      list: JSON.parse(localStorage.getItem("todoList")) || [],
      getSel: 'all'
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  methods: {
    delItem(id) {
      this.list.splice(id, 1)
    },
    addItem(task) {
      this.list.push({ id: this.list.length + 1, name: task, isDone: false })
    },
    changeType(type) {
      this.getSel = type
    },
    clearCompleted() {
      this.list = this.list.filter(item =>item.isDone == false)
    }
  },
  computed: {
    showList() {
      if (this.getSel === 'yes') {
        return this.list.filter(obj => obj.isDone === true)
      }else if (this.getSel === 'no') {
        return this.list.filter(obj => obj.isDone === false)
      }else{
        return this.list
      }
    },
  },

  watch: {
    list: {
      handler() {
        localStorage.setItem('todoList', JSON.stringify(this.list))
      },
      deep: true
    }
  },
  
}
</script>

<style></style>
